package com.kpmg.datalake.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kpmg.datalake.common.dao.PersonParamsSettingMapper;
import com.kpmg.datalake.common.enums.DicTypeEnum;
import com.kpmg.datalake.common.enums.PersonParamEnum;
import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.enums.UseOrDelEnum;
import com.kpmg.datalake.common.exception.BusinessException;
import com.kpmg.datalake.common.model.PersonParamsSetting;
import com.kpmg.datalake.common.service.IComDictService;
import com.kpmg.datalake.common.service.IPersonParamsSettingService;
import com.kpmg.datalake.common.utils.AssertUtil;
import com.kpmg.datalake.common.utils.ContextHolderUtils;
import com.kpmg.datalake.common.utils.ExceptionUtil;
import com.kpmg.datalake.common.vo.PersonParamVO;
import com.kpmg.datalake.common.vo.ServerResponse;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 个性化参数设置 服务实现类
 * </p>
 *
 * @author tzhang13
 * @since 2018-08-21
 */
@Service
public class PersonParamsSettingServiceImpl
    extends ServiceImpl<PersonParamsSettingMapper, PersonParamsSetting>
    implements IPersonParamsSettingService {

  private static final Logger logger =
      LoggerFactory.getLogger(PersonParamsSettingServiceImpl.class);
  @Resource
  private IComDictService comDictService;

  @Override
  public Page<PersonParamVO> getPersonParamPageByType(Page<PersonParamVO> page, String dictType,
      String locale, String userName) {
    AssertUtil.assertNotEmpty(dictType, "字典类型不能为空");
    AssertUtil.assertNotEmpty(locale, "语言不能为空");
    AssertUtil.assertNotEmpty(userName, "用户名不能为空");
    PersonParamVO personParamVO = new PersonParamVO();
    personParamVO.setDictType(dictType);
    personParamVO.setLangCd(locale.toLowerCase());
    personParamVO.setUdtUsrId(userName);
    return page.setRecords(baseMapper.selectPersonParamList(page, personParamVO));
  }

  @Override
  public Page<PersonParamVO> getPersonParamPageByType(Page<PersonParamVO> page, String locale,
      PersonParamVO personParamVO) {
    AssertUtil.assertNotEmpty(locale, "语言不能为空");
    AssertUtil.assertNotEmpty(personParamVO.getUserName(), "用户名不能为空");
    PersonParamVO personParamVO1 = new PersonParamVO();
    personParamVO1.setLangCd(locale.toLowerCase());
    personParamVO1.setUserName(ContextHolderUtils.getUserName());
    personParamVO1.setDictName(personParamVO.getInputName());
    return page.setRecords(baseMapper.selectPersonParamList(page, personParamVO1));
  }

  @Override
  public ServerResponse<String> saveOrUpdateParam(PersonParamsSetting personParamSetting) {

    try {
      // 判断ppsId是否为空
      if (StringUtils.isEmpty(personParamSetting.getPpsId())) {
        personParamSetting.setUserName(ContextHolderUtils.getUserName());
        personParamSetting.setDelInd(UseOrDelEnum.USE.getIntCode());
        super.insert(personParamSetting);
      } else {
        PersonParamsSetting personParamSetting1 = new PersonParamsSetting();
        personParamSetting1.setPpsId(personParamSetting.getPpsId());
        personParamSetting1.setParamsValue(personParamSetting.getParamsValue());
        super.updateById(personParamSetting1);
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      return ExceptionUtil.getExceptionMsg(e);
    }
    return ServerResponse.createBySuccess();
  }

  /**
   * 通过类型获取参数
   *
   * @param type type
   * @return String
   */
  @Override
  public String getPersonParamsByType(String type) {
    AssertUtil.assertNotEmpty(type, "参数类型不能为空");
    PersonParamsSetting setting = new PersonParamsSetting();
    setting.setUserName(ContextHolderUtils.getUserName());
    setting.setDelInd(UseOrDelEnum.USE.getIntCode());
    setting.setParamsType(type);
    PersonParamsSetting personParamsSetting = super.selectOne(new EntityWrapper<>(setting));
    if (personParamsSetting == null) {
      throw new BusinessException(ResultCodeEnum.CODE4000, "未找到对应个性化参数");
    }
    return personParamsSetting.getParamsValue();
  }


  @Override
  public ServerResponse<String> getPersonParam(PersonParamsSetting personParamSetting) {
    try {
      String paramsType = PersonParamEnum.AcceptLang.getCode();
      if (StringUtils.isNotBlank(personParamSetting.getParamsType())) {
        Map<String, String> dictMapByType = comDictService
            .getViewDictMapByType(DicTypeEnum.PERSON_PARAM.getCode());
        if (!dictMapByType.containsKey(personParamSetting.getParamsType())) {
          throw new BusinessException(ResultCodeEnum.CODE4033, ResultCodeEnum.CODE4033.getDisplay());
        }
        paramsType = personParamSetting.getParamsType();
      }
      return ServerResponse.createBySuccess(this.getPersonParamsByType(paramsType));
    } catch (Exception e) {
      return ExceptionUtil.getExceptionMsg(e);
    }
  }

  /**
   * vba 个性化设置保存
   *
   * @param personParamSetting personParamSetting
   * @return ServerResponse
   */
  @Override
  public ServerResponse<String> vbaSavePersonParams(PersonParamsSetting personParamSetting) {
    try {
      AssertUtil.assertNotEmpty(personParamSetting.getParamsType(), "参数类型不能为空");
      Map<String, String> dictMapByType = comDictService
          .getViewDictMapByType(DicTypeEnum.PERSON_PARAM.getCode());
      if (!dictMapByType.containsKey(personParamSetting.getParamsType())) {
        throw new BusinessException(ResultCodeEnum.CODE4033, ResultCodeEnum.CODE4033.getDisplay());
      }
      Map<String, String> paramsValue = comDictService
          .getViewDictMapByType(personParamSetting.getParamsType());
      if (!paramsValue.containsKey(personParamSetting.getParamsValue())) {
        throw new BusinessException(ResultCodeEnum.CODE4033, ResultCodeEnum.CODE4033.getDisplay());
      }
      PersonParamsSetting setting = new PersonParamsSetting();
      setting.setUserName(ContextHolderUtils.getUserName());
      setting.setParamsType(personParamSetting.getParamsType());
      PersonParamsSetting paramsSetting = super.selectOne(new EntityWrapper<>(setting));
      if (paramsSetting == null) {
        paramsSetting = new PersonParamsSetting();
        paramsSetting.setUserName(ContextHolderUtils.getUserName());
        paramsSetting.setParamsType(personParamSetting.getParamsType());
        paramsSetting.setParamsValue(personParamSetting.getParamsValue());
        paramsSetting.setDelInd(UseOrDelEnum.USE.getIntCode());
        if (!super.insert(paramsSetting)) {
          throw new BusinessException(ResultCodeEnum.CODE0001, "保存失败");
        }
      } else {
        paramsSetting.setParamsValue(personParamSetting.getParamsValue());
        if (!super.updateById(paramsSetting)) {
          throw new BusinessException(ResultCodeEnum.CODE0001, "更新失败");
        }
      }
    } catch (BusinessException e) {
      logger.error(e.getMessage(), e);
      return ExceptionUtil.getExceptionMsg(e);
    }
    return ServerResponse.createBySuccess();
  }


}
